//判断登陆设备
//#ifdef H5
var device_type = 'web'
//#endif

//#ifdef APP-PLUS
var device_type = 'mobile'
//#endif

//#ifdef MP
var device_type = 'wxapp'
//#endif

var app = getApp();

//替换为自己的域名,小程序需要https
var host = "http://127.0.0.1:8087";  // 后端调试用的本地环境
// if(process.env.NODE_ENV === 'development'){
//     console.log('开发环境');
// 	host = "https://oms.xianzhiloushop.com:8087"  // 前端调用测试环境8087
// }else{
//     console.log('生产环境');
// 	host = "https://oms.xianzhiloushop.com:8443"  // 正式环境 8443
// }
// var socketUrl = "wss://hrai.online:39005/"; //替换为自己的websocket地址,小程序需要wss


module.exports = {
	// socketUrl:socketUrl,
	HOST: host,
	API_ROOT: host,
	DeviceType: device_type,
	post(options) {
		this.request(options);
	},
	delete(options) {
		options.method = 'DELETE';
		this.request(options);
	},
	get(options) {
		options.method = 'GET';
		this.request(options);
	},
	put(options) {
		options.method = 'PUT';
		this.request(options);
	},
	request(options) {
		var apiRoot = this.API_ROOT;
		var token = '';
		var cookies = '';
        var refreshToken= '';
        var retoken='';
		try {
			token = uni.getStorageSync('token')
            refreshToken = uni.getStorageSync('refreshToken')
            retoken = token           
		} catch (e) {
			// Do something when catch error
		}
        console.log('option',options)
		uni.request({
			url: apiRoot + options.url,
			data: options.data,
			method: options.method ? options.method : 'POST',
			header: {
				'Authorization': 'Bearer '+retoken,
			},
			success: res => {
				var data = res.data;
				if (data.status == 502) {                  
                    uni.request({
                    	url: apiRoot+'/auth/token',
                        header:{
                            // 'Content-Type' :'application/json',
                            'Authorization':refreshToken,
                        },
                        method:'GET',
                    	data: {                   		
                            refreshToken: refreshToken
                    	},
                    	success: data => {
                            let res1=JSON.stringify(data.data)
                            let res=JSON.parse(res1)
                    		if (res.status == 200) {
                                uni.setStorageSync('token',res.data.accessToken)
                    		} else {
                    			console.log('data',res)
                    		}
                    	},
                    	fail: res => {
                    		console.log('刷新失败！');                   		
                    	}
                    })
                    uni.navigateBack({
                        delta:1
                    })
				} else {
					options.success(data);
				}
			},
			fail: function(res) {
				if (options.fail) {
					options.fail(res)
				}
			},
			complete: options.complete ? options.complete : null
		})                         
	}, 
	uploadFile(options) {		
		options.url = this.API_ROOT + options.url;
		let token = this.getToken();
		let that = this;
		let oldSuccess = options.success;
		options.success = function(res) {
			let data = JSON.parse(res.data);
			if (data.status === 505) {
				that.login();
			} else {
				oldSuccess(data);
			}
		}

		options.header = {
			// 'XX-Token': token,
			// 'XX-Device-Type': device_type,
            'Content-Type' :'multipart/form-data',
			'Authorization':'Bearer '+ uni.getStorageSync('token'),
		};
		uni.uploadFile(options);

	},
	getToken() {
		var token = '';
		try {
			token = uni.getStorageSync('token')
		} catch (e) {
			// Do something when catch error
		}

		return token;
	}
};
